var express = require("express");
var router = express.Router();
var request = require('request');
var qs = require('qs');
var path = require('path');

router.get('/',function(req,res) {//主页及atm状态查询
    if(req.session.token) {
        req.session.destroy();//用户返回登陆界面时销毁session
    }
    console.log(new Date());
    var url = 'http://39.106.9.239/ATM/tokens/checkStatus';
    var formData = {
        "deviceNo" : "100"
    }
    request({
        url: url,
        method: "post",
        headers: {
            "content-type" : "application/x-www-form-urlencoded"
        },
        body: qs.stringify(formData)
    },function(_err,_res,_resBody) {
        var body = JSON.parse(_resBody);
        if(!_err && _res.statusCode == 200) {
            switch(body.code) {
                case 0://正常
                res.sendFile(path.join(__dirname,"../src/templates/index.html"));
                break;
                case 711://维修
                res.sendFile(path.join(__dirname,"../src/templates/NeedFix.html"));
                break;
            }
        }
    })
});

router.get('/frozen',function(req,res) { //冻结
    res.sendFile(path.join(__dirname,"../src/templates/frozen.html"));
});

router.get('/loading',function(req,res) { //loading
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/loading.html"));
    }else {
        res.redirect("/");
    }
});
module.exports = router;